#    Copyright 2010 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update
import COOPGIS_FormMisc, COOPGIS_FormInventoryCollection, COOPGIS_FormGeography, COOPGIS_FormAssembly
import COOPGIS_FormSubstation
import COOPGIS_DynamicTemplate
import os, csv, shutil, datetime, time, reportlab, djangoproject.settings, random
from django.contrib.gis.db import models
from django.contrib.gis.geos import *
from django.http import HttpResponse
from django.contrib import admin
from django.template import Context, loader
import djangoproject.settings
import _mysql as mysql
from models import *
from django import forms
from django.db import transaction


@transaction.commit_on_success
def SubstationMainMenu(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress, DefaultMap=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=True, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    Substations=Substation.objects.all().order_by('Name')
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationmainmenu.html')
    c = Context({'LoginStatus':LoginStatus, 'Substations': Substations})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationEquipmentReport(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentreport.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    SubstationEquipments=SubstationEquipment.objects.all().order_by('id')
    Temp=[]
    Temp.append('Substation'.ljust(25) + 'Circuit Name'.ljust(45) + 'Inventory Number'.ljust(35) + 'Serial Number'.ljust(25) + 
                'Model Number'.ljust(45) + 'Catalog Number'.ljust(25))
    for i in SubstationEquipments:
        Row=[]
        Substation=i.GetSubstation()
        if(Substation):
            SubstationName=Substation.Name
        else:
            SubstationName='-'
        Circuit=i.GetSubstationCircuitAssignment()
        if(Circuit):
            CircuitName=Circuit.Name
        else:
            CircuitName='-'
        if(i.SubstationEquipmentModelType):
            ModelNumber=i.SubstationEquipmentModelType.Name
        else:
            ModelNumber='-'
        

        Row.append(SubstationName.ljust(25) + CircuitName.ljust(45) + i.InventoryNumber.ljust(35) + i.SerialNumber.ljust(25) + ModelNumber.ljust(45) +
                   i.CatalogNumber.ljust(25))
        Temp.append(''.join(Row))
    Preformatted='\n'.join(Temp)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentreport.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted':Preformatted})
    return HttpResponse(t.render(c))



@transaction.commit_on_success
def SubstationDescriptionReport(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationdescriptionreport.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    SubstationInspections=substationmodel.SubstationEquipmentInspection.objects.all().order_by('-CreateDateTime')
    Temp=[]
    Temp.append('Substation'.ljust(20) + 'Inspection Date'.ljust(17) + 'Equipment ID'.ljust(15) + 'Circuit'.ljust(40) + 'Description')

    for i in SubstationInspections:
        if(i.Description):
            Temp.append(str(i.SubstationEquipment.GetSubstation()).ljust(20) + str(i.CreateDateTime)[:10].ljust(17) + str(i.id).ljust(15) + 
                        str(i.SubstationEquipment.GetSubstationCircuitAssignment()).ljust(40) + i.Description)
    Preformatted='\n'.join(Temp)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationdescriptionreport.html')
    c = Context({'LoginStatus' : LoginStatus, 'Preformatted':Preformatted})
    return HttpResponse(t.render(c))





@transaction.commit_on_success
def InspectionChoices(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationinspectionchoices.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'InspectionChoice')
    inspectionChoice=COOPGIS_FormSubstation.ProcessInspectionChoiceForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'InspectionChoice')
    form=COOPGIS_FormSubstation.PopulateInspectionChoiceForm(form, inspectionChoice)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationinspectionchoices.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def EquipmentModelType(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentmodeltype.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'EquipmentModelType')
    equipmentModelType=COOPGIS_FormSubstation.ProcessEquipmentModelTypeForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'EquipmentModelType', SpecialBuildFormInfo=equipmentModelType)
    form=COOPGIS_FormSubstation.PopulateEquipmentModelTypeForm(form, equipmentModelType)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentmodeltype.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))


@transaction.commit_on_success
def SubstationSetup(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationsetup.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationSetup')
    substation=COOPGIS_FormSubstation.ProcessSubstationSetupForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationSetup', SpecialBuildFormInfo=substation)
    form=COOPGIS_FormSubstation.PopulateSubstationSetupForm(form, substation)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationsetup.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationEquipmentTemplateSetup(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmenttemplate.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentTemplateSetup')
    substationEquipmentTemplate=COOPGIS_FormSubstation.ProcessSubstationEquipmentTemplateForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentTemplateSetup', SpecialBuildFormInfo=substationEquipmentTemplate)
    form=COOPGIS_FormSubstation.PopulateSubstationEquipmentTemplateForm(form, substationEquipmentTemplate)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmenttemplate.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationEquipmentSetup(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipment.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    try:
        a=SubstationEquipment.objects.all()[0]
    except:
        a=None

    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentSetup', SpecialBuildFormInfo=a)
    substationEquipment=COOPGIS_FormSubstation.ProcessSubstationEquipmentForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentSetup', SpecialBuildFormInfo=substationEquipment)
    form=COOPGIS_FormSubstation.PopulateSubstationEquipmentForm(form, substationEquipment)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipment.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationEquipmentTest(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmenttest.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentTest')
    substationEquipmentTest=COOPGIS_FormSubstation.ProcessSubstationEquipmentTestForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentTest', SpecialBuildFormInfo=substationEquipmentTest)
    form=COOPGIS_FormSubstation.PopulateSubstationEquipmentTestForm(form, substationEquipmentTest)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmenttest.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationInspection(request, Substationid):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    try:
        substation=Substation.objects.filter(id=Substationid)[0]
    except:
        substation=None
    if(not applicationPermission or not substation):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationinspection.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationInspection')
    substationInspection=COOPGIS_FormSubstation.ProcessSubstationInspectionForm(form, UserName=UserNameOrIPAddress, substation=substation)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationInspection', SpecialBuildFormInfo=substationInspection)
    form=COOPGIS_FormSubstation.PopulateSubstationInspectionForm(form, substationInspection)
    substationEquipmentAll=SubstationEquipment.objects.all().order_by('InventoryNumber')
    substationEquipment=[]
    for i in substationEquipmentAll:
        if(i.GetSubstation() == substation):
            substationEquipment.append(i)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationinspection.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form, 'Substation':substation, 'SubstationEquipment':substationEquipment})
    return HttpResponse(t.render(c))

@transaction.commit_on_success
def SubstationEquipmentInspection(request, Substationid, SubstationEquipmentid):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    try:
        substation=Substation.objects.filter(id=Substationid)[0]
    except:
        substation=None
    try:
        substationEquipment=SubstationEquipment.objects.filter(id=SubstationEquipmentid)[0]
    except:
        substationEquipment=None
    if(not applicationPermission or not substation or not substationEquipment):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentinspection.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    substation=Substation.objects.filter(id=Substationid)[0]
    substationInspection=None
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentInspection', SpecialBuildFormInfo=[substationInspection, substation])
    substationEquipmentInspection=COOPGIS_FormSubstation.ProcessSubstationEquipmentInspectionForm(form, UserName=UserNameOrIPAddress, substation=substation, 
                                                                                                  substationEquipment=substationEquipment)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentInspection', SpecialBuildFormInfo=[substationEquipmentInspection, substation])
    form=COOPGIS_FormSubstation.PopulateSubstationEquipmentInspectionForm(form, substationEquipmentInspection)
    if(substationEquipment):
        substationEquipmentTemplate=substationEquipment.SubstationEquipmentTemplate
    else:
        substationEquipmentTemplate=None
    substationEquipmentInspections=djangoproject.models.substationmodel.SubstationEquipmentInspection.objects.filter(SubstationEquipment=substationEquipment).order_by('-CreateDateTime')
    InspectionList=[]
    count = 0
    for i in substationEquipmentInspections:
        InspectionList.append(str(i.CreateDateTime)[:10] + ':   ' + str(i.OperationCounter) + '  ')
        if(count > 5):
            break
    InspectionListString=''.join(InspectionList)
    COOPGIS_DynamicTemplate.UpdateSubstationEquipmentInspectionTemplate(UserNameOrIPAddress, substationEquipmentTemplate)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentinspection' + UserNameOrIPAddress + '.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form, 'Substation': substation.Name, 'SubstationEquipment': str(substationEquipment),
                 'InspectionListString':  InspectionListString})
    return HttpResponse(t.render(c))


@transaction.commit_on_success
def SubstationEquipmentSetting(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentsetting.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentSetting')
    substationEquipmentSetting=COOPGIS_FormSubstation.ProcessSubstationEquipmentSettingForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationEquipmentSetting', SpecialBuildFormInfo=substationEquipmentSetting)
    form=COOPGIS_FormSubstation.PopulateSubstationEquipmentSettingForm(form, substationEquipmentSetting)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationequipmentsetting.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))


@transaction.commit_on_success
def SubstationCircuit(request):
    ApplicationName=djangoproject.settings.COOPGISSubstationApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/substationcircuit.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationCircuit')
    substationCircuit=COOPGIS_FormSubstation.ProcessSubstationCircuitForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'SubstationCircuit', SpecialBuildFormInfo=substationCircuit)
    form=COOPGIS_FormSubstation.PopulateSubstationCircuitForm(form, substationCircuit)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/substationcircuit.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))








@transaction.commit_on_success
def GetRecentMetersCheckedIn(ShowAllIndicator):
    temp=WarehouseMeterInventoryCheckIn()
    Headings=temp.GetHeadings()
    meters=WarehouseMeterInventoryCheckIn.objects.filter(CheckOutDateTime__isnull=True).exclude(JunkIndicator=True).order_by('-CheckInDateTime')
    if(meters.count > 100 and (not ShowAllIndicator)):
        meters=meters[:100]
    StringList=[]
    StringList.append(Headings)
    for i in meters:
        StringList.append(str(i))
    return StringList

@transaction.commit_on_success
def GetRecentMetersCheckedOut(ShowAllIndicator):
    temp=WarehouseMeterInventoryCheckIn()
    Headings=temp.GetHeadings()
    meters=WarehouseMeterInventoryCheckIn.objects.filter(CheckOutDateTime__isnull=False).exclude(JunkIndicator=True).order_by('-CheckOutDateTime')
    if(meters.count > 100 and (not ShowAllIndicator)):
        meters=meters[:100]
    StringList=[]
    StringList.append(Headings)
    for i in meters:
        StringList.append(str(i))
    return StringList

@transaction.commit_on_success
def GetRecentMetersJunked(ShowAllIndicator):
    temp=WarehouseMeterInventoryCheckIn()
    Headings=temp.GetHeadings()
    meters=WarehouseMeterInventoryCheckIn.objects.filter(JunkIndicator=True).order_by('-JunkDateTime')
    if(meters.count > 100 and (not ShowAllIndicator)):
        meters=meters[:100]
    StringList=[]
    StringList.append(Headings)
    for i in meters:
        StringList.append(str(i))
    return StringList



@transaction.commit_on_success
def WarehouseMeterInventoryCheckInOut(request):
    ApplicationName=djangoproject.settings.COOPGISWarehouseApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    LoginStatus = 'Welcome to Warehouse Meter Inventory Checkin.'
    #This application is intentionally usable without loggin in
    #if(not applicationPermission):
    #    t = loader.get_template('/opt/coopgis/djangoproject/templates/warehousemeterinventorycheckin.html')
    #    c = Context({'LoginStatus' : LoginStatus})
    #    return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'meterinventorycheckin')
    form, ShowAllIndicator=COOPGIS_FormSubstation.ProcessWarehouseMeterInventoryCheckInForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'meterinventorycheckin')
    form=COOPGIS_FormSubstation.PopulateWarehouseMeterInventoryCheckInForm(form)
    MetersCheckedInStringList=GetRecentMetersCheckedIn(ShowAllIndicator)
    MetersCheckedOutStringList=GetRecentMetersCheckedOut(ShowAllIndicator)
    MetersJunkedStringList=GetRecentMetersJunked(ShowAllIndicator)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/warehousemeterinventorycheckin.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form, 'MetersCheckedInStringList':MetersCheckedInStringList, 
                 'MetersJunkedStringList':MetersJunkedStringList, 'MetersCheckedOutStringList':MetersCheckedOutStringList})
    return HttpResponse(t.render(c))






@transaction.commit_on_success
def WarehouseMeterInspectionChoices(request):
    ApplicationName=djangoproject.settings.COOPGISWarehouseApplicationName
    UserNameOrIPAddress=COOPGIS_Misc.GetUserNameOrIPAddress(request, GetDefaultMap=False, ApplicationName=ApplicationName)
    LoginStatus, applicationPermission=COOPGIS_Misc.CheckLoginPermissions(request, ApplicationName)
    if(not applicationPermission):
        t = loader.get_template('/opt/coopgis/djangoproject/templates/warehousemeterinspectionchoices.html')
        c = Context({'LoginStatus' : LoginStatus})
        return HttpResponse(t.render(c))
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'meterinspectionchoice')
    inspectionChoice=COOPGIS_FormSubstation.ProcessWarehouseMeterInspectionChoiceForm(form, UserName=UserNameOrIPAddress)
    form=COOPGIS_FormMisc.BuildForm(request, ApplicationName + 'meterinspectionchoice')
    form=COOPGIS_FormSubstation.PopulateWarehouseMeterInspectionChoiceForm(form, inspectionChoice)
    t = loader.get_template('/opt/coopgis/djangoproject/templates/warehousemeterinspectionchoices.html')
    c = Context({'LoginStatus' : LoginStatus, 'form':form})
    return HttpResponse(t.render(c))


